import java.util.Stack;

public class Sort {

    //插入排序
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int ret = arr[i];
            int j = i - 1;
            for (; j >= 0 ; j--) {
                if(arr[j] > ret) {
                    arr[j+1] = arr[j];
                }else {
                    //arr[j+1] = ret;
                    break;
                }
            }
            arr[j+1] = ret;
        }
    }

    //希尔排序
    public static void shellSort(int[] arr) {
        int gap = arr.length / 2;
        while(gap > 0) {
            shell(arr,gap);
            gap /= 2;
        }
    }

    public static void shell(int[] arr,int gap) {
        for (int i = gap; i < arr.length; i++) {
            int ret = arr[i];
            int j = i - gap;
            for (; j >= 0 ; j -= gap) {
                if(arr[j] > ret) {
                    arr[j+gap] = arr[j];
                }else {
                    //arr[j+1] = ret;
                    break;
                }
            }
            arr[j+gap] = ret;
        }
    }

    //选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int mindex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[mindex] > arr[j]) {
                    mindex = j;
                }
            }
            //交换
            swap(arr,mindex,i);
        }
    }

    //选择排序2
    public static void selectSort2(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while(left < right) {
            int mindex = left;
            int maxdex = left;
            for (int i = left + 1; i <= right ; i++) {
                if(arr[mindex] > arr[i]) {
                    mindex = i;
                }
                if(arr[maxdex] < arr[i]) {
                    maxdex = i;
                }
            }
            swap(arr,mindex,left);
            ////假设第一个值 是最大值
            if(maxdex == left) {
                maxdex = mindex;
            }
            swap(arr,maxdex,right);
            left++;
            right--;
        }
    }

    //堆排序
    public static void heapSort(int[] arr) {
        createHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr,0,end);
            siftDown(arr,0,end);
            end--;
        }
    }

    //建立大根堆
    public static void createHeap(int[] arr) {
        for (int i = (arr.length-1-1) / 2; i >= 0 ; i--) {
            siftDown(arr,i, arr.length);
        }
    }

    //向下调整
    private static void siftDown(int[] arr,int parent, int k) {
        int child = 2 * parent + 1;
        while(child < k) {
            if(child + 1 < k && arr[child + 1] > arr[child]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                //交换
                swap(arr,parent,child);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] arr) {
        //趟数
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flu = false;
            //每一趟的比较的次数
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                    flu = true;
                }
            }
            if( !flu) {
                break;
            }
        }
    }


    //快速排序霍尔法
    public static void quickSortHoare(int[] arr) {
        quick(arr,0,arr.length - 1);
    }

    private static void quick(int[] arr, int start, int end) {
        if(start >= end) {
            return;
        }

        int par = parrtion(arr,start,end);

        //往左走
        quick(arr,start,par - 1);

        //往右走
        quick(arr,par + 1,end);

    }

    private static int parrtion(int[] arr, int left, int right) {

        int i = left;
        int ret = arr[left];

        while(left < right) {
            while(left < right && arr[right] >= ret) {
                right--;
            }
            while(left < right && arr[left] <= ret) {
                left++;
            }
            //交换
            swap(arr,left,right);
        }
        //交换
        swap(arr,i,left);

        return left;
    }

    //快速排序挖坑法
    public static void quickSort2(int[] arr) {
        quick(arr,0,arr.length - 1);
    }

    private static void quick2(int[] arr, int start, int end) {
        if(start >= end) {
            return;
        }

        int par = parrtion2(arr,start,end);

        //往左走
        quick2(arr,start,par - 1);

        //往右走
        quick2(arr,par + 1,end);

    }

    private static int parrtion2(int[] arr, int left, int right) {

        int ret = arr[left];

        while(left < right) {
            while(left < right && arr[right] >= ret) {
                right--;
            }

            arr[left] = arr[right];

            while(left < right && arr[left] <= ret) {
                left++;
            }

            arr[right] = arr[left];

        }
        arr[left] = ret;

        return left;
    }

    //快速排序非递归
    public static void quickSortNor(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int par = parrtion3(arr,left,right);

        Stack<Integer> stack = new Stack<>();

        //左边有两个元素及以上
        if(left + 1 < par) {
            stack.push(left);
            stack.push(par - 1);
        }

        //右边有两个元素及以上
        if(par < right - 1) {
            stack.push(par + 1);
            stack.push(right);
        }

        while(!stack.isEmpty()) {
             right = stack.pop();
             left = stack.pop();

             par = parrtion3(arr,left,right);

            //左边有两个元素及以上
            if(left + 1 < par) {
                stack.push(left);
                stack.push(par - 1);
            }

            //右边有两个元素及以上
            if(par < right - 1) {
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    private static int parrtion3(int[] arr, int left, int right) {

        int ret = arr[left];

        while(left < right) {
            while(left < right && arr[right] >= ret) {
                right--;
            }

            arr[left] = arr[right];

            while(left < right && arr[left] <= ret) {
                left++;
            }

            arr[right] = arr[left];

        }
        arr[left] = ret;

        return left;
    }

    //归并排序
    public static void mergeSort(int[] arr) {
        mergeSortChild(arr,0,arr.length-1);
    }
    private static void mergeSortChild(int[] arr,int left,int right) {
        if(left == right) {
            return;
        }

        int mid = (left + right) / 2;

        mergeSortChild(arr,left,mid);

        mergeSortChild(arr,mid + 1,right);

        //开始合并
        merge(arr,left,mid,right);
    }

    public static void feidigui(int[] arr) {
        int gap = 1;
        while(gap < arr.length) {
            for (int i = 0; i < arr.length; i = i + 2*gap) {

                int left = i;
                int mid = left + gap - 1;

                if(mid >= arr.length) {
                    mid = arr.length-1;
                }

                int right = mid+gap;

                if(right >= arr.length) {
                    right = arr.length-1;
                }

                merge(arr,left,mid,right);
            }

            gap *= 2;
        }
    }
    private static void merge(int[] arr, int left, int mid, int right) {

        //临时数组
        int[] tmpArr = new int[right - left + 1];
        int k = 0;

        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;

        while(s1 <= e1 && s2 <= e2) {
            if(arr[s1] <= arr[s2]) {
                tmpArr[k++] = arr[s1++];
            }else {
                tmpArr[k++] = arr[s2++];
            }
        }

        while(s1 <= e1) {
            tmpArr[k++] = arr[s1++];
        }

        while(s2 <= e2) {
            tmpArr[k++] = arr[s2++];
        }

        //拷贝临时数组给到arr
        for (int i = 0; i < k; i++) {
            arr[i + left] = tmpArr[i];
        }

    }



    private static void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }


    //计数排序
    public static void countSort(int[] arr) {

        //取得原始数组最大值和最小值
        int max = arr[0];
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i] < min) {
                min = arr[i];
            }
            if(arr[i] > max) {
                max = arr[i];
            }
        }

        //建立计数数组
        int len = max - min + 1;
        int[] countArray = new int[len];


        //遍历原始数组
        for (int i = 0; i < arr.length; i++) {
            int index = arr[i]-min;
            countArray[index]++;
        }

        //遍历计数数组给到原始数组
        int k = 0;
        for (int i = 0; i < countArray.length; i++) {
            while(countArray[i] != 0) {
                arr[k] = i+min;
                k++;
                countArray[i]--;
            }
        }
    }



//    public static void countSort22(int[] array) {
//        //1. 求最大值 和 最小值 O(N)
//        int max = array[0];
//        int min = array[0];
//        for (int i = 1; i < array.length; i++) {
//            if(array[i] < min) {
//                min = array[i];
//            }
//            if(array[i] > max) {
//                max = array[i];
//            }
//        }
//        //2.定义计数数组 并且求出长度
//        int len = max - min + 1;
//        int[] countArray = new int[len];
//        //3.遍历原始数组 计数数组开始计数 O(N)
//        for (int i = 0; i < array.length; i++) {
//            int index = array[i] - min;
//            countArray[index]++;
//        }
//
//        //4.遍历计数数组
//        int k = 0;//array数组的新下标  O(范围)
//        for (int i = 0; i < countArray.length; i++) {
//            while (countArray[i] != 0) {
//                array[k] = i+min;
//                k++;
//                countArray[i]--;
//            }
//        }
//        //执行到这里 array数组全部都有序了
//    }


}
